package md.mru.core.common;


import org.dozer.CustomFieldMapper;
import org.dozer.classmap.ClassMap;
import org.dozer.fieldmap.FieldMap;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.proxy.HibernateProxy;

public class LazyFieldMapper implements CustomFieldMapper {

    @Override
    public boolean mapField(Object source, Object destination, Object sourceFieldValue, ClassMap classMap, FieldMap fieldMapping) {
        if ((sourceFieldValue instanceof HibernateProxy) ||
                (sourceFieldValue instanceof AbstractPersistentCollection) && !((AbstractPersistentCollection) sourceFieldValue).wasInitialized()) {
            destination = null;
            return true;
        }


        //not Mapped, let Dozer map as usual
        return false;
    }
}
